<?php

namespace App\Admin\Forms\Setting;

use App\Models\About as Model;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class Site extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '基础设置';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        $data = $request->all([
            'beian',
        ]);

        foreach ($data as $field => $value) {
            Model::set(Model::TYPE_SITE, $field, $value);
        }

        admin_success('保存成功.');

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('beian', 'ICP 备案号')->rules('required');
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $data = Model::whereType(Model::TYPE_ABOUT)
            ->pluck('meta_value', 'meta_key')->toArray();

        return [
            'beian' => $data['beian'] ?? "京ICP-1234567890",
        ];
    }
}
